<?php
/**
 * Author: Nguyen Hoai Tan (nguoiao007@gmail.com)
 * 
 * @category   default 
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 * 
 */
require_once 'modules/comment/models/Comment.php';
require_once 'modules/content/models/ContentCategory.php';
require_once 'modules/content/models/Content.php';

class comment_AdminController extends Nine_Controller_Action_Admin 
{
    
    public function managerAction()
    {
        /**
         * Check permission
         */
        if (false == $this->checkPermission('see_comment', null, '?')) {
            $this->_forwardToNoPermissionPage();
            return;
        }
        
        $this->view->menu = array('comment', 'manager');
        
        
        $config = Nine_Registry::getConfig();
        $numRowPerPage = Nine_Registry::getConfig("defaultNumberRowPerPage");
        $currentPage = $this->_getParam("page",1);
        $displayNum = $this->_getParam('displayNum', false);
        
        $objComment     = new Models_Comment();
        $objCat          = new Models_ContentCategory();
        
        /**
         * Get number of items per page
         */
        if (false === $displayNum) {
            $displayNum = $this->session->contentDisplayNum;
        } else {
            $this->session->contentDisplayNum = $displayNum;
        }
        if (null != $displayNum) {
            $numRowPerPage = $displayNum;
        }
        /**
         * Get condition
         */
        $condition = $this->_getParam('condition', false);
        if (false === $condition) {
            $condition = $this->session->contentCondition;
        } else {
            $this->session->contentCondition = $condition;
            $currentPage = 1;
        }
        if (false == $condition) {
            $condition = array();
        }
        
        /**
         * Get all contents
         */
        
         
        $allComments = $objComment->setAllLanguages(true)->getAllComments($condition,  'comment_id DESC',
                                                   $numRowPerPage,
                                                   ($currentPage - 1) * $numRowPerPage
                                                  );
//        echo '<pre>';print_r($allComments);die;
        /**
         * Count all contents
         */
//		foreach ($allComments as &$item) {
//        	$item['comment'] = strip_tags($item['comment'],'&nbsp;');
//        	if(strlen($item['comment']) > 40) {
//        		$cut = substr($item['comment'],0,40);
//        		$item['comment'] = substr($cut, 0, strrpos($cut, ' ')). "...";
//        	}
//        }
//        unset($item);
//		$this->view->allCats = $objCat->getAll(array('sorting ASC'))->toArray();
        $count = count($objComment->setAllLanguages(true)->getallComments($condition));
        $this->setPagination($numRowPerPage, $currentPage, $count);
        $this->view->allComments = $allComments;
        $this->view->commentMessage = $this->session->commentMessage;
        $this->session->commentMessage = null;
        $this->view->condition = $condition;
        $this->view->displayNum = $numRowPerPage;
        $this->view->fullPermisison = $this->checkPermission('see_comment');
    }
    
    public function editAction()
    {
     	if (false == $this->checkPermission('edit_comment', null, '?')) {
            $this->_forwardToNoPermissionPage();
            return;
        }
    	$id = $this->_getParam('id', false);
//    	echo '<pre>'; print_r($id);die;
     	if (false == $id) {
            $this->_redirect('comment/admin/manager');
        }
		$data = $this->_getParam('data',false);
//		echo '<pre>'; print_r($data);die;
		if(false != $data) {
			$objComment = new Models_Comment();
			$objComment->update($data, array('comment_id=?' => $id));
            $this->session->commentMessage = array(
                                       'success' => true,
                                       'message' => Nine_Language::translate("Edit comment successfully")
                                   );
			$this->_redirect('comment/admin/manager');                                   
		} else {
			$objComment = new Models_Comment();
			$data = $objComment->find($id)->toArray();
//			echo '<pre>'; print_r($data);die;
			$data = current($data);
//			echo '<pre>'; print_r($data);die;
			if (false == $data) {
				$this->session->commentMessage = array(
                                               'success' => false,
                                               'message' => Nine_Language::translate("Comment doesn't exist.")
                                           );
				$this->_redirect('comment/admin/manager');
			}
		}
		$this->view->data = $data;
//		$this->view->commentMessage = $this->session->commentMessage;
//		$this->session->commentMessage = null;
		$this->view->headTitle(Nine_Language::translate('Edit Comment'));
		$this->view->fullPermisison = $this->checkPermission('edit_comment', null, '*');
    }

    public function enableCommentAction()
    {
        /**
         * Check permission
         */
        if (false == $this->checkPermission('edit_comment', null, '*')) {
            $this->_forwardToNoPermissionPage();
            return;
        }
        $id = $this->_getParam('id', false);
        
        if (false == $id) {
            $this->_redirect('comment/admin/manager');
        }
        
        $ids = explode('_', trim($id, '_'));
        
        $objComment = new Models_Comment();
        try {
            foreach ($ids as $id) {
               $objComment->update(array('enabled' => 1), array('comment_id=?' => $id));
            }
            $this->session->commentMessage = array(
                                               'success' => true,
                                               'message' => Nine_Language::translate('Enable comment successfully')
                                           );
        } catch (Exception $e) {
            $this->session->commentMessage = array(
                                               'success' => false,
                                               'message' => Nine_Language::translate('Can NOT enable this comment. Please try again')
                                           );
        }
        $this->_redirect('comment/admin/manager');
    }

    
    
    public function disableCommentAction()
    {
        /**
         * Check permission
         */
        if (false == $this->checkPermission('edit_comment', null, '*')) {
            $this->_forwardToNoPermissionPage();
            return;
        }
        $id = $this->_getParam('id', false);
        
        if (false == $id) {
            $this->_redirect('comment/admin/manager');
        }
        
        $ids = explode('_', trim($id, '_'));
        
        $objComment = new Models_Comment();
        try {
            foreach ($ids as $id) {
               $objComment->update(array('enabled' => 0), array('comment_id=?' => $id));
            }
            $this->session->commentMessage = array(
                                               'success' => true,
                                               'message' => Nine_Language::translate('Disable content successfully')
                                           );
        } catch (Exception $e) {
            $this->session->commentMessage = array(
                                               'success' => false,
                                               'message' => Nine_Language::translate('Can NOT disable this comment. Please try again')
                                           );
        }
        $this->_redirect('comment/admin/manager');
    }

   
    
    public function deleteCommentAction()
    {
        /**
         * Check permission
         */
        if (false == $this->checkPermission('delete_comment')) {
            $this->_forwardToNoPermissionPage();
            return;
        }
        $id = $this->_getParam('id', false);
        
        if (false == $id) {
            $this->_redirect('comment/admin/manager');
        }
        
        $ids = explode('_', trim($id, '_'));
        
        $objComment = new Models_Comment();
        try {
            foreach ($ids as $id) {
               $objComment->delete(array('comment_id=?' => $id));
            }
            $this->session->commentMessage = array(
                                               'success' => true,
                                               'message' => Nine_Language::translate('Delete comment successfully')
                                           );
        } catch (Exception $e) {
            $this->session->commentMessage = array(
                                               'success' => false,
                                               'message' => Nine_Language::translate('Can NOT delete this comment. Please try again')
                                           );
        }
        $this->_redirect('comment/admin/manager');
    }
    
	public function disableGeneralCommentAction()
    {
        /**
         * Check permission
         */
        if (false == $this->checkPermission('edit_comment', null, '*')) {
            $this->_forwardToNoPermissionPage();
            return;
        }
        $id = $this->_getParam('id', false);
        
        if (false == $id) {
            $this->_redirect('comment/admin/manager');
        }
        
        $ids = explode('_', trim($id, '_'));
        
        $objComment = new Models_Comment();
        try {
            foreach ($ids as $id) {
               $objComment->update(array('enabled' => 0), array('comment_id=?' => $id));
            }
            $this->session->commentMessage = array(
                                               'success' => true,
                                               'message' => Nine_Language::translate('Disable comment successfully')
                                           );
        } catch (Exception $e) {
            $this->session->commentMessage = array(
                                               'success' => false,
                                               'message' => Nine_Language::translate('Can NOT disable this comment. Please try again')
                                           );
        }
        $this->_redirect('comment/admin/manager');
    }
    
	public function enableGeneralCommentAction()
    {
        /**
         * Check permission
         */
        if (false == $this->checkPermission('edit_comment', null, '*')) {
            $this->_forwardToNoPermissionPage();
            return;
        }
        $id = $this->_getParam('id', false);
        
        if (false == $id) {
            $this->_redirect('comment/admin/manager');
        }
        
        $ids = explode('_', trim($id, '_'));
        
        $objComment = new Models_Comment();
        try {
            foreach ($ids as $id) {
               $objComment->update(array('enabled' => 1), array('comment_id=?' => $id));
            }
            $this->session->commentMessage = array(
                                               'success' => true,
                                               'message' => Nine_Language::translate('Enable comment successfully')
                                           );
        } catch (Exception $e) {
            $this->session->commentMessage = array(
                                               'success' => false,
                                               'message' => Nine_Language::translate('Can NOT enable this comment. Please try again')
                                           );
        }
        $this->_redirect('comment/admin/manager');
    }
    
	public function deleteGeneralCommentAction()
    {
        /**
         * Check permission
         */
        if (false == $this->checkPermission('delete_comment')) {
            $this->_forwardToNoPermissionPage();
            return;
        }
        $id = $this->_getParam('id', false);
        
        if (false == $id) {
            $this->_redirect('comment/admin/manager');
        }
        
        $ids = explode('_', trim($id, '_'));
        
        $objComment = new Models_Comment();
        try {
            foreach ($ids as $id) {
               $objComment->delete(array('comment_id=?' => $id));
            }
            $this->session->commentMessage = array(
                                               'success' => true,
                                               'message' => Nine_Language::translate('Delete comment successfully')
                                           );
        } catch (Exception $e) {
            $this->session->commentMessage = array(
                                               'success' => false,
                                               'message' => Nine_Language::translate('Can NOT delete this comment. Please try again')
                                           );
        }
        $this->_redirect('comment/admin/manager');
    }
}